<?php
namespace Shopadmin\Controller;
use Think\Controller;
class LoginController extends Controller{
    /**
     * 登录
     */
	public function index(){
	    if(IS_POST){
            $account = I('account', '');
            $password = I('password', '', 'md5');
            if(empty($account)||empty($password)){
                $this->ajaxReturn(array('success'=>false, 'msg'=>'请输入帐号密码'));
            }
            $shop = M("shop")->where(array('account'=>$account))->find();
            /**
            检测账户是否可用
             *
             */
            if($shop){  //检测账户是否过期
                $state=$shop['state'];
                if($state==0){
                    $this->ajaxReturn(array('success'=>false, 'msg'=>'您的账号未开通,请联系管理员开通'));
                }
                $now=time();
                $expire_time=$shop['expiry_time'];
                if($now>$expire_time){
                    $this->ajaxReturn(array('success'=>false, 'msg'=>'您的账号已经过期,请联系管理员'));
                }
                if($shop['password'] != $password){
                    $this->ajaxReturn(array('success'=>false, 'msg'=>'账号或密码不正确'));

                }else{
                    session('shopid', $shop['id']);
                    $data = array(
                        'end_time'=>time()
                    );
                    M("shop")->where(array('account'=>$account))->save($data);
                    $this->ajaxReturn(array('success'=>true, 'msg'=>'登录成功'));
                }
            }else{
                $this->ajaxReturn(array('success'=>false, 'msg'=>'账号或密码不正确'));
            }
        }else{
	        $adlist=M('advertisement')->select();
            $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
           $domain= $http_type . $_SERVER['HTTP_HOST']."/" ;

           foreach ($adlist as $k=>$v){
               $adlist[$k]['img']=$domain.$v['img'];
           }
            $this->assign("adlist",$adlist);
	        $this->display();
        }
	}
	public function ad_detail(){
	    $id=I("get.id");
	    $detail=M('advertisement')->where(array('id'=>$id))->find();
        $this->assign("detail", $detail);
        $this->display();

    }

    /**
     * 退出登录
     */
	public function logout(){
		session('shopid', NULL);
		$this->redirect('Shopadmin/Login/index');
	}
    /**
     * 修改密码
     */
	public function changePwd(){
        $shopid = session("shopid");
        if(empty($shopid)){
            header("location:/Shopadmin/login/index");
        }
	    if(IS_POST){
            $password = I('pwd', '', 'md5');
            $newpassword = I('newpwd', '', 'md5');
            $shopid = session('shopid');

            if(!empty($shopid)){
                $nowpassword = M("shop")->where(array('id'=>$shopid))->getField('password');
                if($nowpassword != $password){
                    $this->ajaxReturn(array('success'=>false, 'msg'=>'原密码不正确'));
                }
                if($nowpassword==$newpassword){
                    $this->ajaxReturn(array('success'=>false, 'msg'=>'原密码和新密码不能相同'));
                }
                $rs = M("shop")->where(array('id'=>$shopid))->setField('password', $newpassword);
                if($rs){
                    $this->ajaxReturn(array('success'=>true, 'msg'=>'修改成功'));
                }
            }else{
                $this->redirect("Shopadmin/Login/index");
            }
        }else{
            $shop_id=session("shopid");
            $info=M('shop')->where(array('id'=>$shop_id))->find();
            $this->assign("info", $info);
            $this->display();
        }
	}
	//查看商户信息
    public function info(){
        $shopid = session("shopid");
        if(empty($shopid) ){
            header("location:/Shopadmin/login/index");
        }
	    $info=M("shop")->where("id=$shopid")->find();
	    $this->assign("info",$info);
	    $this->display();
    }
}